﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ServiceController : ControllerBase
    {
        private readonly PetDbContext _DB;
        public ServiceController(PetDbContext _context)
        {
            _DB = _context;

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Pet_Service> GetAll()
        {
            return _DB.Pet_Service.ToList();
        }


        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddPet([FromBody] Pet_Service service)
        {
            Result result = new Result();
            _DB.Pet_Service.Add(service);
            result.Data = _DB.SaveChanges();
            return result;
        }


        /// <summary>
        /// 改信息
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UpPet([FromBody] Pet_Service service)
        {
            Result result = new Result();
            _DB.Pet_Service.Update(service);
            result.Data = _DB.SaveChanges();
            return result;
        }


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelPet(Guid id)
        {
            Result result = new Result();

            Pet_Service? service = _DB.Pet_Service.Where(w => w.ID == id).SingleOrDefault();
            if (service != null)
                _DB.Pet_Service.Remove(service);
            result.Data = _DB.SaveChanges();
            return result;
        }

    }
}

